<?php
header('Content-type:text/html;charset=utf8');
/**
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-4-30
*/
/**
 * 
 * 广告控制器
 * @author xjh
 *
 */
class AdverController extends Controller{
	/**
	 * 访问控制过滤
	 * @author xjh
	 */
	function filters(){
		return array(
			'accessControl'
		);
	}
	/**
	 * 具体方法访问具体条件
	 */
	
	function accessRules(){
		return array(
			array(
				'allow',
				'actions'=>array(''),
				'users'=>array('@'),//登陆系统的用户
			),
//			array(
//				'deny',
//				'users'=>array('*'),//所有用户
//			),
		);
	}

	/**
	 * 新品上市顶部信息
	 * 
	 */
	function actionadvernewtop(){
		$this->renderPartial('advernewtop');
	}
	
	/**
	 * 新品上市信息
	 */
	function actionadvernew(){
		$adver_model = Adver::model();
		if(@$_POST['send'] == '更新信息'){
			for ($i=0; $i < count($_POST['adver_id']); $i++) { 
				$row = $adver_model -> updateAdver($_POST['adver_id'][$i],$_POST['sort'][$i]);
			}
		}
		if(@$_POST['btquery'] == '查询'){
			$infos = $adver_model->pageSearch($_POST);
		}else{
			$infos = $adver_model->advernew();
		}
		$infos_count = $adver_model->infos_count;
		
		$active = array(
			0=>'否',
			1=>'是',
		);
		
		$position = array(
			1=>'新品上市',
			2=>'横条广告',
			3=>'品牌汇总',
		);
		$obj = new CDbConnection();
		$topInfo = $obj -> getStats();
		$number = $topInfo[0];
		$times = $topInfo[1];
		$this->renderPartial('advernew',array('infos'=>$infos,'active'=>$active,'info_count'=>$infos_count[0]['count(*)'],'number'=>$number,'times'=>$times,'position'=>$position));
	}
	
	
/**
	 * 品牌汇总顶部信息
	 * 
	 */
	function actionadverbrandtop(){
		$this->renderPartial('adverbrandtop');
	}
	
	/**
	 * 品牌汇总信息
	 */
	function actionadverbrand(){
		$adver_model = Adver::model();
		if(@$_POST['send'] == '更新信息'){
			for ($i=0; $i < count($_POST['adverbrand_id']); $i++) { 
				$row = $adver_model -> updateAdverBrand($_POST['adverbrand_id'][$i],$_POST['sort'][$i]);
			}
		}
		if(@$_POST['btquery'] == '查询'){
			$infos = $adver_model->pageSearchBrand($_POST);
		}else{
			$infos = $adver_model->adverbrand();
		}
		$infos_count = $adver_model->infos_count;
		
		$active = array(
			0=>'否',
			1=>'是',
		);
		
		$position = array(
			1=>'第一列',
			2=>'第二列',
			3=>'第三列',
			4=>'第四列',
		);
		$obj = new CDbConnection();
		$topInfo = $obj ->getStats();
		$number = $topInfo[0];
		$times = $topInfo[1];
		$this->renderPartial('adverbrand',array('infos'=>$infos,'active'=>$active,'info_count'=>$infos_count[0]['count(*)'],'number'=>$number,'times'=>$times,'position'=>$position));
	}

	/**
	 * 广告删除
	 */
	function actionAdverDel($adver_id){
		$adver_model = Adver::model();
		$row = $adver_model->AdverDel($adver_id);
		if($row){
			echo 1;
			exit;
		}else{
			echo 0;
			exit;
		}
	}
	
	
/**
	 * 品牌汇总广告删除
	 */
	function actionAdverBrandDel($adverbrand_id){
		$adver_model = Adver::model();
		$row = $adver_model->AdverBrandDel($adverbrand_id);
		if($row){
			echo 1;
			exit;
		}else{
			echo 0;
			exit;
		}
	}

	/**
	 * 新品上市广告+汇总头部修改
	 */
	function actionadverupd($adver_id){
		
		$adver_model = new Adver();
		$adver_info = $adver_model->getAdverById($adver_id);
		if(isset($_POST) && @$_POST['btsave'] == "修改"){
			$row = $adver_model->AdverUpdate($_POST,$adver_id);
			if($row){
				$this->redirect("./index.php?r=backweb/adver/advernew");
			}else{
				echo "<script>alert('修改失败');</script>";
			}
		}
		$this->renderPartial('updateadver',array('adver_info'=>$adver_info[0]));
	}
	
	/**
	 * 品牌汇总列表修改
	 */
	function actionadverbrandupd($adverbrand_id){
		$adver_model = new Adver();
		$adver_info = $adver_model->getAdverBrandById($adverbrand_id);
		if(isset($_POST) && @$_POST['btsave'] == "修改"){
			$row = $adver_model->AdverBrandUpdate($_POST,$adverbrand_id);
			if($row){
				$this->redirect("./index.php?r=backweb/adver/adverbrandtop");
			}else{
				echo "<script>alert('修改失败');</script>";
			}
		}
		$this->renderPartial('updateadverbrand',array('adver_info'=>$adver_info[0]));
	}
	
	/**
	 * 是否有效的切换
	 */
	function actionActive($adver_id,$active_id){
		$active = new active();
		$active -> setActive($adver_id,$active_id,"adver","adver_id");
		$this->redirect("./index.php?r=backweb/adver/advernew");
	}

	/**
	 * 是否有效的切换
	 */
	function actionBrandActive($adverbrand_id,$active_id){
		$active = new active();
		$active -> setActive($adverbrand_id,$active_id,"adverbrand","adverbrand_id");
		$this->redirect("./index.php?r=backweb/adver/adverbrand");
	}
	/**
	 * 是否推荐的切换
	 */
	function actionRec($adver_id,$adver_rec){
		$active = new Adver();
		$active -> setRec($adver_id,$adver_rec);
		$this->redirect("./index.php?r=backweb/adver/advernew");
	}
	
	function actiontoAddAdver(){
		$this->renderPartial('adveradd');
	}
	
	function actiontoAddAdverBrand(){
		$this->renderPartial('adverbrandadd');
	}
	
	/*
	 * 新品上市广告添加控制器
	 * @author xjh
	 */
	function actionAddAdver(){
	
		$image = new Image($_POST['adver_thumbnail_url']);
		if($_POST['adver_rec'] == 1){
			$image->fixPro(372,270);
		}else{
			if($_POST['adver_position'] == 1){
				$image->fixPro(287,150);
			}elseif($_POST['adver_position'] == 2){
				$image->fixPro(1218,90);
			}elseif($_POST['adver_position'] == 3){
				$image->fixPro(280,300);
			}
		}
		$image->out();
		$adver_model = new Adver();
		$infos = $adver_model->checkExist($_POST);
			if($_POST['btsave'] == '添加'){
					if(!empty($infos)){
						echo "<script>alert('广告已存在');</script>";
						echo "操作错误，请返回";
						exit;
					}
					if($adver_model->addAdver($_POST)){
						echo "<script>alert('广告添加成功');</script>";
						 $this->renderPartial('advernewtop');
					}else{
						echo "<script>alert('添加失败');</script>";
					}
			}else{
				echo "非法操作,请返回继续操作";
				exit;
			}
	}
	
	

	/*
	 * 新品上市广告添加控制器
	 * @author xjh
	 */
	function actionAddAdverBrand(){
		$image = new Image($_POST['adverbrand_thumbnail_url']);
		$image->fixPro(80,80);
		$image->out();
		$adver_model = new Adver();
		$infos = $adver_model->checkExistBrand($_POST);
			if($_POST['btsave'] == '添加'){
					if(!empty($infos)){
						echo "<script>alert('广告已存在');</script>";
						echo "操作错误，请返回";
						exit;
					}
					if($adver_model->addAdverBrand($_POST)){
						echo "<script>alert('广告添加成功');</script>";
						 $this->renderPartial('adverbrandtop');
					}else{
						echo "<script>alert('添加失败');</script>";
					}
			}else{
				echo "非法操作,请返回继续操作";
				exit;
			}
	}
	
	//单文件上传
	function actionthumbnail(){
		$this->renderPartial('thumbnail');
	}
	//单文件上传处理
	function actionuploadfile(){ 
		if(isset($_POST['send'])){
			$_fileupload = new upload('pic',$_POST['MAX_FILE_SIZE']);
			$this->alertOpenerClose("缩略图上传成功",$_fileupload->getPath());
		}else{
			echo "<script>alert('文件过大,请重新上传')</script>";
			exit;
		}
	}
	//上传成功返回填写地址
	function alertOpenerClose($_info,$_path){
		echo "<script type='text/javascript'>alert('$_info');</script>";
		echo "<script type='text/javascript'>opener.document.fm.adver_thumbnail_url.value='$_path';</script>";
		echo "<script type='text/javascript'>opener.document.fm.adverbrand_thumbnail_url.value='$_path';</script>";
		echo "<script type='text/javascript'>opener.document.fm.pic.style.display='block';</script>";
		echo "<script type='text/javascript'>opener.document.fm.pic.src='$_path';</script>";
		echo "<script type='text/javascript'>window.close()</script>";
		exit();
	}
}
?>